<?php

	function sendContent()
	{
		// default, starting variables
		$user = "";
		$pass = "";
		$post = "";
		$reply = "";

		// Variables, taken from the user form			
		if($_COOKIE['twitterWriteUser'] != NULL)
			$user = $_COOKIE['twitterWriteUser'];
		
		if($_COOKIE['twitterWritePass'] != NULL)
			$pass = $_COOKIE['twitterWritePass'];
			
		if($_COOKIE['twitterWritePost'] != NULL)
			$post = $_COOKIE['twitterWritePost'];
			
		$postURL = "update.xml?status=".urlencode(stripslashes(urldecode($post)));
		
		$result = postToTwitter($user, $pass, $postURL);

    	// set success/failure message
    	if(substr($result, 0, 8) != "/statuses")
    	{
        		$reply = "OK! Posted to ".$user."'s feed.";
    	} 
    	else
    	{
        		$reply = "Error posting to Twitter!";
    	}

		// return the success/failure
		echo $reply;
		
	}
	
	function postToTwitter($username, $password, $postContent)
	{
	   $out  = "POST /statuses/" . $postContent . " HTTP/1.1\r\n";
	   $out .= "Host: twitter.com\r\n";
	   $out .= "Connection: Close\r\n";
	   $out .= "Authorization: Basic ".base64_encode($username . ":" . $password)."\r\n";
	   $out .= "\r\n";
	
	   if (!$conex = fsockopen("twitter.com", 80, $errno, $errstr, 30))
	       return 0;
	   fwrite($conex, $out);
	   $data = '';
	   while (!feof($conex))
	   {
	       $data .= fgets($conex, 512);
	   }
	   fclose($conex);
	   
	   $result_arr = explode("Connection: close\r\n\r\n", $data);
	   $data = $result_arr[1];
	   
	   return $data;
	}

	sendContent();

?>